<?php
/**
 * 配置类
 * 
 * Copyright (c) 2014~2019 https://desion.net All rights reserved.
 * Author: 阿林 QQ:120867629 <linhong00@gmail.com>
 */
declare (strict_types = 1);
namespace dsphp;

class Config
{
    // 配置参数
    protected $config = [];
    // 配置文件目录
    protected $path = '/';


    /**
     * 构造方法
     */
    public function __construct($path)
    {
        if (is_dir($path)) {
            if (substr($path, -1) != DIRECTORY_SEPARATOR) {
                $path .= DIRECTORY_SEPARATOR;
            }

            $this->path = $path;
            $files = glob($path . '*' . DSPHP_EXT);
    
            foreach ($files as $file) {
                $this->load($file, pathinfo($file, PATHINFO_FILENAME));
            }
        }
    }

    /**
     * 加载配置文件
     *
     * @param  string $file 配置文件名
     * @param  string $name 一级配置名
     */
    public function load(string $file, string $name = ''): array
    {
        if (is_file($file)) {
            $filename = $file;
        } elseif (is_file($this->path . $file . DSPHP_EXT)) {
            $filename = $this->path . $file . DSPHP_EXT;
        } else {
            $filename = '';
        }

        if ($filename) {
            $config = include $filename;

            return is_array($config) ? $this->set($config, strtolower($name)) : [];
        }

        return $this->config;
    }

    /**
     * 检测配置是否存在
     *
     * @param  string $name 配置参数名（支持多级配置 .号分割）
     * @return bool
     */
    public function has(string $name): bool
    {
        return !is_null($this->get($name));
    }

    /**
     * 获取一级配置
     *
     * @param  string $name 一级配置名
     * @return array
     */
    protected function pull(string $name): array
    {
        $name = strtolower($name);
        return $this->config[$name] ?? [];
    }

    /**
     * 获取配置参数 为空则获取所有配置
     *
     * @param  string $name    配置参数名（支持多级配置 .号分割）
     * @param  mixed  $default 默认值
     * @return mixed
     */
    public function get(string $name = null, $default = null)
    {
        // 无参数时获取所有
        if (empty($name)) {
            return $this->config;
        }

        if (false === strpos($name, '.')) {
            return $this->pull($name);
        }

        $name   = strtolower($name);
        $name   = explode('.', $name);
        $config = $this->config;

        // 按.拆分成多维数组进行判断
        foreach ($name as $val) {
            if (isset($config[$val])) {
                $config = $config[$val];
            } else {
                return $default;
            }
        }
        
        return $config;
    }

    /**
     * 设置配置参数 name为数组则为批量设置
     *
     * @param  array  $config 配置参数
     * @param  string $name   配置名
     */
    public function set(array $config, string $name = ''): array
    {
        $config = array_change_key_case($config);

        if ($name) {
            if (isset($this->config[$name])) {
                $result = array_merge($this->config[$name], $config);
            } else {
                $result = $config;
            }

            $this->config[$name] = $result;
        } else {
            $result = $this->config = array_merge($this->config, $config);
        }

        return $result;
    }
}
